/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.Routes;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.LibreGeoSocial.Api.GeoNode;
import com.LibreGeoSocial.Api.GooglePlaceMark;
import com.LibreGeoSocial.App.R;


public class GoogleRoutesList extends ListActivity {
	
	
	RouteAdapter mAdapter;  
	
	static ArrayList<GeoNode> nodeList;

	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);


		Intent i = getIntent();
		
        GoogleRoutes myRoute = new GoogleRoutes();
        
        myRoute.setSource( LocationService.getCurrentLocation().getLatitude(), 
        				   LocationService.getCurrentLocation().getLongitude());
        
        
        myRoute.setDestination( i.getDoubleExtra("latitude", Integer.MIN_VALUE),
        						i.getDoubleExtra("longitude", Integer.MIN_VALUE));
        
        myRoute.setLanguage("es");
        
        nodeList = myRoute.getRoute(); 
        
        
  		// Adapter for ListView
        mAdapter = new RouteAdapter(this);
        setListAdapter(mAdapter);

		
	}	

    
	public static class RouteAdapter extends BaseAdapter {

		private Context mContext;
		
		public RouteAdapter(Context c) {
            mContext = c;
        }

		public int getCount() {
			// TODO Auto-generated method stub
			return nodeList.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.routes, null);
	        } else {
	            // Use convertView if it is available
	            view = convertView;
	        }
			
	         
	        GeoNode node = nodeList.get(position);
	        
	        if ( GooglePlaceMark.class.isInstance(node) )    	
        	{
        		GooglePlaceMark place = (GooglePlaceMark) nodeList.get(position);
        	        
		        
		        TextView t = (TextView) view.findViewById(R.id.name);
		        t.setText(place.getName());	    
				
				t = (TextView) view.findViewById(R.id.distance);
				t.setText(place.getDescription());
        	}	
			return view;
	        
		}

	}



}
